#include <QImage>
#include <vector>
#include <QString>
#include <QColor>
using namespace std;

vector<vector<unsigned short>> imageToArray(QString path)
{
    QImage image;
    image.load(path);
    image=image.convertToFormat(QImage::Format_Grayscale8, Qt::AutoColor);

    vector<vector<unsigned short>> array;
    QSize size=image.size();
    for(int i=0;i<size.width();i++)
    {
        vector<unsigned short> line;
        for(int j=0;j<size.height();j++)
        {
            QColor qc(image.pixel(i,j));
            line.push_back(qc.red());
        }
        array.push_back(line);
    }

    return array;
}

QImage imageToArray(vector<vector<unsigned short>>& array)
{
    int width=array.size();
    int height=array[0].size();
    QImage image(width,height,QImage::Format_Grayscale8);

    for(int i=0;i<width;i++)
    {
        for(int j=0;j<height;j++)
        {
            unsigned short val=array[i][j];
            image.setPixel(i,j,qRgb(val,val,val));
        }
    }

    return image;
}

int main()
{
    auto a=imageToArray("Z:/123.PNG");
    //对a做一些处理
    QImage i=imageToArray(a);
    i.save("Z:/235.PNG","PNG",100);
}